package cruciverba.file01;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;

public class FileWriterObserver implements Observer {

	private Observable observable;
	private ILineWriter lineWriter;
	private String fileName;

	public FileWriterObserver(Observable observable, ILineWriter lineWriter,
			String fileName) {
		super();
		this.observable = observable;
		this.lineWriter = lineWriter;
		this.fileName = fileName;
		observable.addObserver(this);
	}

	private void writeFile() {

		try {
			FileWriter writer = new FileWriter(fileName, true);
			writer.write(lineWriter.composeLineToWrite());
			writer.close();

		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	@Override
	public void update(Observable o, Object arg) {
		writeFile();
	}

}
